package com.qhx.rtkcloud.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class NtripServerMock {

   static String authInfo = "SOURCE letmein /mount1\n" +
            "Source-Agent: NTRIP NtripServerCMD/1.0\n\n";

    public static void main(String[] args) throws IOException {
        Socket client  = new Socket("localhost", 1101);
        InputStream inputStream = client.getInputStream();
        OutputStream outputStream = client.getOutputStream();
        outputStream.write(authInfo.getBytes());
        outputStream.flush();
        byte[] bytes = new byte[10240];
        int length = 0;
        while(true) {
            if  ((length = inputStream.read(bytes)) > 0) {
                String info = new String(Arrays.copyOfRange(bytes, 0, length));
                System.out.println(info);
//                if (info.startsWith("ICY 200 OK")) {
                    Executors.newScheduledThreadPool(1)
                            .scheduleAtFixedRate(() -> {
                                try {
                                    outputStream.write(new byte[]{1,2,3,4,5,6});
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }

                            }, 1, 1, TimeUnit.SECONDS);
//                }

                new Thread(() -> {
                        try {
                            int len = 0;
                            while (((len = inputStream.read(bytes)) > 0)) {
                                System.out.println(Arrays.toString(Arrays.copyOfRange(bytes, 0, len)));

                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                }).start();


            }
        }

    }
}
